﻿<UserControl x:Class="Fluffy.Archive.ArchiveDocumentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:archive="clr-namespace:Fluffy.Archive"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:behaviors="clr-namespace:Fluffy.Library.Behaviors;assembly=Fluffy.Library"
             xmlns:behaviors1="clr-namespace:Fluffy.Archive.Behaviors"
             xmlns:comparers="clr-namespace:Fluffy.Archive.Comparers"
             xmlns:views="clr-namespace:Fluffy.Library.Views;assembly=Fluffy.Library"
             mc:Ignorable="d" d:DataContext="{d:DesignInstance archive:ArchiveDocumentViewModel}"
             d:DesignHeight="300" d:DesignWidth="600">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="250"/>
            <ColumnDefinition Width="5"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TreeView Grid.Column="0" ItemsSource="{Binding RootFolderAsArray}" x:Name="TreeView" BorderThickness="0" x:FieldModifier="private" Margin="3,3,0,3">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type archive:ArchiveEntityViewModel}" ItemsSource="{Binding Folders}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding Icon}"/>
                        <TextBlock Text="{Binding DisplayName}" Margin="5,0"/>
                    </StackPanel>
                </HierarchicalDataTemplate>
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded}"/>
                    <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
                </Style>
            </TreeView.Resources>
            <i:Interaction.Behaviors>
                <behaviors:DragDropBehavior Command="{Binding DragDropForFileCommand}" CommandParameter="{Binding ElementName=TreeView, Path=SelectedItem}"/>
                <behaviors1:BindableSelectedItemBehavior SelectedItem="{Binding SelectedFolder, Mode=OneWayToSource}"/>
            </i:Interaction.Behaviors>
            <TreeView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Copy" Command="{Binding CopyCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}"/>
                    <Separator/>
                    <MenuItem Header="Extract Selected Items..." Command="{Binding ExtractForFileCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem}"/>
                    <MenuItem Header="Extract All..." Command="{Binding ExtractAllCommand}"/>
                </ContextMenu>
            </TreeView.ContextMenu>
        </TreeView>
        <GridSplitter Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <GridSplitter.Template>
                <ControlTemplate>
                    <Border Background="Transparent">
                        <Rectangle Width="1" Fill="#99CCCCCC" HorizontalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </GridSplitter.Template>
        </GridSplitter>
        <DataGrid Grid.Column="2" ItemsSource="{Binding FilesNames}" SelectionMode="Extended" x:Name="ListView" BorderThickness="0" x:FieldModifier="private" AutoGenerateColumns="False"
                  GridLinesVisibility="None" IsReadOnly="True" HeadersVisibility="Column" SelectionUnit="FullRow" Background="{x:Static SystemColors.WindowBrush}"
                  EnableRowVirtualization="False" EnableColumnVirtualization="False">
            <DataGrid.Resources>
                <Style x:Key="ColumnHeaderGripperStyle" TargetType="{x:Type Thumb}">
                    <Setter Property="Width" Value="8" />
                    <Setter Property="Background" Value="Transparent" />
                    <Setter Property="Cursor" Value="SizeWE" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Thumb}">
                                <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="VerticalContentAlignment" Value="Center" />
                    <Setter Property="Background" Value="Transparent" />
                    <Setter Property="Height" Value="24" />
                    <Setter Property="BorderThickness" Value="0,0,1,0" />
                    <Setter Property="VerticalAlignment" Value="Center" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Border Grid.ColumnSpan="2" Padding="3,0,3,0" Margin="0,0,-1,0" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="#99CCCCCC" MinHeight="18" VerticalAlignment="{TemplateBinding VerticalAlignment}"/>
                                    <ContentPresenter Grid.Column="0" Margin="4,0" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                    <Thumb Grid.Column="0" Grid.ColumnSpan="2" x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" Style="{StaticResource ColumnHeaderGripperStyle}" />
                                    <Thumb Grid.Column="0" Grid.ColumnSpan="2" x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{StaticResource ColumnHeaderGripperStyle}" />
                                    <Path Grid.Column="1" x:Name="SortArrow" Visibility="Hidden" Data="M0,0 L1,0 0.5,1 z" Stretch="Fill" 
                                        HorizontalAlignment="Right" Width="8" Height="6" Stroke="DarkGray" Fill="LightGray" Margin="8,0" 
                                        VerticalAlignment="Center" RenderTransformOrigin="0.5,0.4" />
                                </Grid>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="SortDirection" Value="Ascending">
                                        <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                                        <Setter TargetName="SortArrow" Property="RenderTransform">
                                            <Setter.Value>
                                                <RotateTransform Angle="180" />
                                            </Setter.Value>
                                        </Setter>
                                    </Trigger>
                                    <Trigger Property="SortDirection" Value="Descending">
                                        <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#FFFAFAFA"/>
                            <Setter Property="BorderThickness" Value="1"/>
                            <Setter Property="VerticalAlignment" Value="Stretch" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Resources>
            <DataGrid.RowStyle>
                <Style TargetType="Control">
                    <Setter Property="Margin" Value="0,1"/>
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.CellStyle>
                <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
                    <Setter Property="BorderThickness" Value="0"/>
                </Style>
            </DataGrid.CellStyle>
            <i:Interaction.Behaviors>
                <behaviors:DragDropBehavior Command="{Binding DragDropCommand}" CommandParameter="{Binding ElementName=ListView, Path=SelectedItems}"/>
                <behaviors:DoubleClickBehavior Command="{Binding OpenSelectedEntityCommand}" CommandParameter="{Binding ElementName=ListView, Path=SelectedItem}"/>
                <behaviors1:BindableSelectedItemsBehavior SelectedItems="{Binding SelectedItems, Mode=OneWayToSource}"/>
                <behaviors:ColumnWithCustomSortingBehavior Column="Name">
                    <behaviors:ColumnWithCustomSortingBehavior.Comparer>
                        <comparers:ArchiveContentNameComparer/>
                    </behaviors:ColumnWithCustomSortingBehavior.Comparer>
                </behaviors:ColumnWithCustomSortingBehavior>
                <behaviors:ColumnWithCustomSortingBehavior Column="Size">
                    <behaviors:ColumnWithCustomSortingBehavior.Comparer>
                        <comparers:ArchiveContentSizeComparer/>
                    </behaviors:ColumnWithCustomSortingBehavior.Comparer>
                </behaviors:ColumnWithCustomSortingBehavior>
                <behaviors:ColumnWithCustomSortingBehavior Column="Modified">
                    <behaviors:ColumnWithCustomSortingBehavior.Comparer>
                        <comparers:ArchiveContentDateComparer/>
                    </behaviors:ColumnWithCustomSortingBehavior.Comparer>
                </behaviors:ColumnWithCustomSortingBehavior>
            </i:Interaction.Behaviors>
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Name" Width="300" SortMemberPath="DisplayName">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Margin="3,0,0,0" Orientation="Horizontal" d:DataContext="{d:DesignInstance archive:IArchiveEntity}">
                                <Image Source="{Binding Icon}"/>
                                <TextBlock Text="{Binding DisplayName}" Margin="5,0,0,0"/>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Size" Binding="{Binding SizeTitle}">
                    <DataGridTextColumn.CellStyle>
                        <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
                            <Setter Property="Block.TextAlignment" Value="Right"/>
                            <Setter Property="BorderThickness" Value="0"/>
                        </Style>
                    </DataGridTextColumn.CellStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Header="Modified" Binding="{Binding LastWriteTimeTitle}"/>
            </DataGrid.Columns>
            <DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Open" Command="{Binding OpenCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}"/>
                    <Separator/>
                    <MenuItem Header="Copy" Command="{Binding CopyCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}"/>
                    <Separator/>
                    <MenuItem Header="Extract Selected Items..." Command="{Binding ExtractCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}"/>
                    <MenuItem Header="Extract All..." Command="{Binding ExtractAllCommand}"/>
                </ContextMenu>
            </DataGrid.ContextMenu>
        </DataGrid>
        <views:WaitView Grid.ColumnSpan="3" DataContext="{Binding}"/>
    </Grid>
</UserControl>
